package org.dromara.demo.service;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoExample1;
import org.springframework.stereotype.Service;
import org.dromara.demo.domain.DemoExample4;
import org.dromara.demo.mapper.DemoExample4Mapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 单表Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class DemoExample4ServiceImpl extends BaseService {

  private final DemoExample4Mapper baseMapper;

  /**
   * 查询单表
   */
  public DemoExample4 queryById(Long id){
    return baseMapper.selectById(id);
  }

  /**
   * 查询单表列表
   */
  public TableDataInfo<DemoExample4> queryPageList(DemoExample4 bo, PageQuery pageQuery) {
    Page<DemoExample4> result = baseMapper.queryPageList(pageQuery.build(), bo);
    return TableDataInfo.build(result);
  }

  /**
   * 查询单表列表
   */
  public List<DemoExample4> queryList(DemoExample4 bo) {
    return baseMapper.queryPageList(bo);
  }

  private LambdaQueryWrapper<DemoExample4> buildQueryWrapper(DemoExample4 bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<DemoExample4> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), DemoExample4::getName, bo.getName());
    lqw.eq(bo.getCustomerId() != null, DemoExample4::getCustomerId, bo.getCustomerId());
    lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
      DemoExample4::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
    return lqw;
  }

  /**
   * 新增单表
   */
  public Boolean insertByBo(DemoExample4 bo) {
    validEntityBeforeSave(bo);
    boolean flag = baseMapper.insert(bo) > 0;
    return flag;
  }

  /**
   * 修改单表
   */
  public Boolean updateByBo(DemoExample4 bo) {
    validEntityBeforeSave(bo);
    return baseMapper.updateById(bo) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(DemoExample4 entity){
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除单表
   */
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if(isValid){
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
